<?php
class ComplaintsModel extends model
{
	static $_instance = null;

	static function getInstance ()
	{
		if (is_null(self::$_instance)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	// 取得常量组
	function Save($p)
	{
		return $this->sql("insert into crm_complaints(`types`,`customs_id`,`input_staff`,`contact`,`agency_id`,`title`,`content`)values([types],[customs_id],[input_staff],'[contact]',[agency_id],'[title]','[content]')",$p)->i();
	}
	
	// 取得派工设备(总件数)
	function getCount($p){
		$sql  = "select count(*) as c from crm_complaints_view where 1=1 ";
	    if($p["types"] != ""){$sql .= " and types='".$p["types"]."'";}
		if($p["status"] != ""){$sql .= " and status='".$p["status"]."'";}
		if($p["title"] != ""){$sql .= " and title like '%".$p["title"]."%'";}
		if($p["agency_id"] != ""){$sql .= " and agency_id in (".$p["agency_id"].")";}
		if($p["updated1"] != ""){$sql .= " and updated>='".$p["updated1"]."'";}
		if($p["updated2"] != ""){$sql .= " and updated<='".$p["updated2"]."'";}
		
		$return = $this->sql($sql)->s(1);
		return $return["c"];
	}
	
	// 取得派工设备(列表)
	function getComplaints($p)
	{
		$sql = "select * from crm_complaints_view where 1=1 ";
		if($p["types"] != ""){$sql .= " and types='".$p["types"]."'";}
		if($p["status"] != ""){$sql .= " and status='".$p["status"]."'";}
		if($p["title"] != ""){$sql .= " and title like '%".$p["title"]."%'";}
		if($p["agency_id"] != ""){$sql .= " and agency_id in (".$p["agency_id"].")";}
		if($p["updated1"] != ""){$sql .= " and updated >= '".$p["updated1"]."'";}
		if($p["updated2"] != ""){$sql .= " and updated <= '".$p["updated2"]."'";}
		//echo $sql;
		$sql .= " order by ".$p["sortField"]." ".$p["sortOrder"];
		$sql .= " limit [start],[end]";
		
		return $this->sql($sql,array("start"=>$p["r"]*$p["p"],"end"=>$p["r"]))->s();
	}
	
	//咨询投诉处理中
	function ComplaintsRun($p){
		$this->sql("update crm_complaints set `status`='[status]' where `id`=[id] ",$p)->u();
	}
	
	//处理结果保存
	function ComplaintsSave($p){		
		$this->sql("update crm_complaints set `process_agency`='[process_agency]',`process_staff`='[process_staff]',`process_result`='[process_result]',`process_time`='[process_time]',`blame`='[blame]' where `id`=[id] ",$p)->u();
	}
}